package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.entity.Goods;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.WxSerchIndexVo;
import com.cskaoyan.controller.wx.service.FootprintService;
import com.cskaoyan.controller.wx.service.WxCollectService;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 *  小程序搜索主页
 *
 */
@RestController
@RequestMapping("wx/search")
public class WxSerchIndexController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    UserService userService;

    @Autowired
    FootprintService footprintService;

    @Autowired
    WxCollectService wxCollectService;

    @RequestMapping("list")
    public BaseRespVo searchList(){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getSession().getAttribute("username");
        Integer userId = userService.getUserId(username);
        // 从足迹中获取最新的浏览商品id,如果没有，就从收藏中获取
        Integer goodsId = footprintService.getNewestGoodsIdByUserId(userId);
        if(goodsId == null){
            // 从收藏中获取
            goodsId = wxCollectService.getNewestGoodsByUserId(userId);
        }
        Goods defaultKeyword = goodsService.selectDefaultKeyword(goodsId);

        // 热门关键字列表
        List<Goods> hotKeywordList = goodsService.getHotKeywordList(defaultKeyword);

        // 历史关键字列表
        String[] historyKeywordList = null;
        WxSerchIndexVo wxSerchIndexVo = new WxSerchIndexVo(defaultKeyword, hotKeywordList, historyKeywordList);
        return BaseRespVo.ok(wxSerchIndexVo);

    }
}
